package cn.tedu.coolsharkmall.service.Impl;

import cn.tedu.coolsharkmall.ex.ServiceException;
import cn.tedu.coolsharkmall.mapper.AlbumMapper;
import cn.tedu.coolsharkmall.pojo.dto.AlbumAddNewDTO;
import cn.tedu.coolsharkmall.pojo.dto.AlbumUpdateDTO;
import cn.tedu.coolsharkmall.pojo.entity.Album;
import cn.tedu.coolsharkmall.pojo.vo.AlbumListItemVo;
import cn.tedu.coolsharkmall.pojo.vo.AlbumStandardVo;
import cn.tedu.coolsharkmall.service.IAlbumService;
import cn.tedu.coolsharkmall.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的业务,参数{}", albumAddNewDTO);
        String name = albumAddNewDTO.getName();
        int count = albumMapper.countByName(name);
        log.debug("根据名称统计数量,结果{}", count);
        if(count > 0){
            log.warn("添加相册失败, 相册名称【{}】已经被占用", name);
            String message = "添加相册失败, 相册名称【"+name+"】已经被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //创建一个Album对象
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO, album);
        //调用Mapper对象的insert()执行插入相册数据
        log.debug("即将插入数据,参数:{}", album);
        albumMapper.insert(album);
        //log.debug("插入数据成功!");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除相册】的业务,id为{}", id);
        AlbumStandardVo albumStandardVo = albumMapper.getStandardById(id);
        log.debug("根据id={}检查相册数据是否存在,结果{}", id, albumStandardVo);
        if(albumStandardVo == null){
            log.warn("删除相册失败, 相册【{}】不存在", id);
            String message = "删除相册失败,尝试删除的相册数据不存在!";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        log.debug("即将删除数据,参数:{}", id);
        albumMapper.deleteById(id);
        //log.debug("删除数据成功!");
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateDTO alAlbumUpdateDTO) {
        log.debug("开始处理【修改相册】的业务，ID:{},新数据{}",id, alAlbumUpdateDTO);
        //Mapper对象的getStandById()执行查询
        AlbumStandardVo albumStandardVo = albumMapper.getStandardById(id);
        log.debug("根据id={}检查相册数据是否存在,结果{}", id, albumStandardVo);
        //判断查询是否为null
        if(albumStandardVo == null){
            //是：抛出异常
            String message = "添加相册失败,尝试修改的相册数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //调用Mapper对象的countByNameAndNotId()执行统计
        int count = albumMapper.countByNameAndNotId(id, alAlbumUpdateDTO.getName());
        //判断统计结果是否大于0
        // 是:抛出异常
        if(count > 0){
            log.warn("添加相册失败, 相册名称【{}】已经被占用", alAlbumUpdateDTO.getName());
            String message = "添加相册失败, 相册名称【"+alAlbumUpdateDTO.getName()+"】已经被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //创建Album对象
        Album album = new Album();
        //复制属性,设置ID
        BeanUtils.copyProperties(alAlbumUpdateDTO, album);
        album.setId(id);
        //调用Mapper对象的update()方法执行修改
        albumMapper.update(album);
    }

    @Override
    public AlbumStandardVo getStandardById(Long id) {
        log.debug("开始处理【根据ID查询相册详情的业务】, 参数{}", id);
        AlbumStandardVo albumStandardVo = albumMapper.getStandardById(id);
        if(albumStandardVo == null){
            log.warn("查询相册失败, 相册【{}】不存在", id);
            String message = "添加相册失败,尝试修改的相册数据不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return albumStandardVo;
    }

    @Override
    public List<AlbumListItemVo> list() {
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<AlbumListItemVo> list = albumMapper.selectAlbumList();
        return list;
    }


}
